/**
 * @author Helio
 */
var LocalXMLFileUtil = function(){
	
	var configFile = "config.xml";
	var configDoc;
	
	var codeFile = "data/DB_BILL_CODE_SUPPORT.xml";
	var codeDoc;
	
	var billFile = "data/DB_SALE_BILL.xml";
	var billDoc;
	
	var bankAccountFile = "data/DB_BANK_ACCOUNT.xml";
	var bankAccountDoc;
	
	var transferBillFile = "data/DB_TRANSFER_BILL.xml";
	var transferBillDoc;
	
	var clientsFile = "data/DB_CLIENTS.xml";
	var clientsDoc;
	
	var productsFile = "data/DB_PRODUCTS.xml";
	var productsDoc;

	var manufacturerFile = "data/DB_MANUFACTURERS.xml";
	var manufacturerDoc;
	
	var orderFile = "data/DB_PURCHASE_ORDER.xml";
	var orderDoc;
	
	var userFile = "data/DB_USER_INFO.xml";
	var userDoc;
	
	var storageFile = "data/DB_STORAGE.xml";
	var storageDoc;
	
	var stockFile = "data/DB_STORAGE_PRODUCT.xml";
	var stockDoc;
	
	var categoryFile = "data/DB_CATEGORIES.xml";
	var categoryDoc;
	
	var classificationFile = "data/DB_CLASSIFICATION.xml";
	var classificationDoc;
	
	var companyFile = "data/DB_COMPANY_DEPT.xml";
	var companyDoc;
	
	var paymentFile = "data/DB_PAYMENT_BILL.xml";
	var paymentDoc;
	
	return {
		loadXML : function(xmlFile){
			var xmlDoc;
			var prefsFile = air.File.applicationDirectory;
			var file = prefsFile.resolvePath(xmlFile);
			var fileStream = new air.FileStream(); 
			try {
				fileStream.open(file, air.FileMode.READ); 		
			
				var prefsXML = fileStream.readUTFBytes(fileStream.bytesAvailable); 
				var domParser = new DOMParser();
				xmlDoc = domParser.parseFromString(prefsXML, "text/xml");	
				fileStream.close();
				
			} catch(err) {
				//如果文件不存在，就创建一个新的。
				xmlDoc = document.createElement("root");
				this.saveXML(xmlDoc,xmlFile);
				xmlDoc = this.loadXML(xmlFile);
			}
			return xmlDoc;
		},
		
		saveXML : function(xmlDoc,xmlFile) {
			var prefsFile = air.File.applicationDirectory;
			var file = prefsFile.resolvePath(xmlFile);
			var saveFile = new air.File(file.nativePath);
			var fileStream = new air.FileStream(); 
			fileStream.open(saveFile, air.FileMode.WRITE);
			
			fileStream.writeUTFBytes(LocalXMLFileUtil.xmlToString(xmlDoc));
			fileStream.close();
		},
		
		xmlToString : function(elem) {	
			var serialized;		
			try {
				// XMLSerializer exists in current Mozilla browsers
				serializer = new XMLSerializer();
				serialized = serializer.serializeToString(elem);
			} catch (e) {
				// Internet Explorer has a different approach to serializing XML
				serialized = elem.xml;
			}
			
			return '<?xml version="1.0" encoding="utf-8"?>' + serialized;
		},
		
		getBillCodeDoc: function() {
			if(!codeDoc) {
				codeDoc = LocalXMLFileUtil.loadXML(codeFile);
			}
			
			return codeDoc;
		},
		
		getBillCodeFile: function() {
			
			return codeFile;
		},
		
		getBillDoc: function() {
			if(!billDoc) {
				billDoc = LocalXMLFileUtil.loadXML(billFile);
			}
			
			return billDoc;
		},
		
		getBillFile: function() {
			
			return billFile;
		},
		
		getBankAccountDoc: function() {
			if(!bankAccountDoc) {
				bankAccountDoc = LocalXMLFileUtil.loadXML(bankAccountFile);
			}
			
			return bankAccountDoc;
		},
		
		getBankAccountFile: function() {
			
			return bankAccountFile;
		},
		
		getTransferBillFile: function() {
			
			return transferBillFile;
		},
		
		getTransferBillDoc: function() {
			if(!transferBillDoc) {
				transferBillDoc = LocalXMLFileUtil.loadXML(transferBillFile);
			}
			
			return transferBillDoc;
		},		
		
		getClientsDoc: function() {
			if(!clientsDoc) {
				clientsDoc = LocalXMLFileUtil.loadXML(clientsFile);
			}
			
			return clientsDoc;
		},
		
		getClientsFile: function() {
			
			return clientsFile;
		},
		
		getCompanyDoc: function() {
			if(!companyDoc) {
				companyDoc = LocalXMLFileUtil.loadXML(companyFile);
			}
			
			return companyDoc;
		},
		
		getCompanyFile: function() {
			
			return companyFile;
		},
		
		getProductsDoc: function() {
			if(!productsDoc) {
				productsDoc = LocalXMLFileUtil.loadXML(productsFile);
			}
			
			return productsDoc;
		},
		
		getProductsFile: function() {
			
			return productsFile;
		},
		
		getManufacturerDoc: function() {
			if(!manufacturerDoc) {
				manufacturerDoc = LocalXMLFileUtil.loadXML(manufacturerFile);
			}
			
			return manufacturerDoc;
		},
		
		getManufacturerFile: function() {
			
			return manufacturerFile;
		},
		
		getOrderDoc: function() {
			if(!orderDoc) {
				orderDoc = LocalXMLFileUtil.loadXML(orderFile);
			}
			
			return orderDoc;
		},
		
		getOrderFile: function() {
			
			return orderFile;
		},
		
		getUserDoc: function() {
			if(!userDoc) {
				userDoc = LocalXMLFileUtil.loadXML(userFile);
			}
			
			return userDoc;
		},
		
		getUserFile: function() {
			
			return userFile;
		},
		
		getStorageDoc: function() {
			if(!storageDoc) {
				storageDoc = LocalXMLFileUtil.loadXML(storageFile);
			}
			
			return storageDoc;
		},
		
		getStorageFile: function() {
			
			return storageFile;
		},
		
		getStockDoc: function() {
			if(!stockDoc) {
				stockDoc = LocalXMLFileUtil.loadXML(stockFile);
			}
			
			return stockDoc;
		},
		
		getStockFile: function() {
			
			return stockFile;
		},
		
		getCategoryDoc: function() {
			if(!categoryDoc) {
				categoryDoc = LocalXMLFileUtil.loadXML(categoryFile);
			}
			
			return categoryDoc;
		},
		
		getCategoryFile: function() {
			
			return categoryFile;
		},
		
		getClassificationDoc: function() {
			if(!classificationDoc) {
				classificationDoc = LocalXMLFileUtil.loadXML(classificationFile);
			}
			
			return classificationDoc;
		},
		
		getClassificationFile: function() {
			
			return classificationFile;
		},
		
		getConfigDoc: function() {
			if(!configDoc) {
				configDoc = LocalXMLFileUtil.loadXML(configFile);
			}
			
			return configDoc;
		},
		
		getConfigFile: function() {
			
			return configFile;
		},
		
		getPaymentDoc: function() {
			if(!paymentDoc) {
				paymentDoc = LocalXMLFileUtil.loadXML(paymentFile);
			}
			
			return paymentDoc;
		},
		
		getPaymentFile: function() {
			
			return paymentFile;
		}
	}
	
}();


LocalXMLFileUtil.configFileUtil = function(){
	
	return {
		saveUrl : function(urlStr) {
			var configDoc = LocalXMLFileUtil.getConfigDoc();
			var root = configDoc.documentElement;
			var serverNode = null;
			
			var serverArray = Ext.query("/server", root);
			
			if(serverArray.length == 0) {
				serverNode = configDoc.createElement("server");
				root.appendChild(serverNode);
			} else {
				serverNode = serverArray[0];
			}
			
			var urlArray = Ext.query("/url", serverNode);
					
			Ext.each(urlArray, function(urlNode) {
				serverNode.removeChild(urlNode);
			});
			
			var node = configDoc.createElement("url");
			var contextNode = configDoc.createCDATASection(urlStr);
			node.appendChild(contextNode);
			serverNode.appendChild(node);
			
			LocalXMLFileUtil.saveXML(configDoc,LocalXMLFileUtil.getConfigFile());		
		},
		
		saveTheme : function(theme) {
			var configDoc = LocalXMLFileUtil.getConfigDoc();
			var root = configDoc.documentElement;
			var custermNode = null;
			
			var custermArray = Ext.query("/custerm", root);
			
			if(custermArray.length == 0) {
				custermNode = configDoc.createElement("custerm");
				root.appendChild(custermNode);
			} else {
				custermNode = custermArray[0];
			}
			
			var themeArray = Ext.query("/theme", custermNode);
					
			Ext.each(themeArray, function(themeNode) {
				custermNode.removeChild(themeNode);
			});
			
			var node = configDoc.createElement("theme");
			var contextNode = configDoc.createCDATASection(theme);
			node.appendChild(contextNode);
			custermNode.appendChild(node);
			
			LocalXMLFileUtil.saveXML(configDoc,LocalXMLFileUtil.getConfigFile());		
		},
		
		initInfo : function() {
			if(!this.configInfo) {
				this.configInfo = {};
			
				var configDoc = LocalXMLFileUtil.getConfigDoc();
				var root = configDoc.documentElement;
				
				var objArray = Ext.query("/server/url/*", root);
				Ext.each(objArray, function(url) {
					this.configInfo["url"] = url.nodeValue;
					if(url.nodeValue.indexOf("service/") > 0) {
						this.configInfo["home"] = url.nodeValue.replace("service/", "");
					} else {
						this.configInfo["home"] = url.nodeValue.replace("service", "");
					}
				}.createDelegate(this));
			
				var objArray = Ext.query("/custerm/theme/*", root);
				Ext.each(objArray, function(theme) {
					this.configInfo["theme"] = theme.nodeValue;
				}.createDelegate(this));
			}
			
			return this.configInfo;
		}
	}
}();
